package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Merchant;
import com.tjetc.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("admin/merchant")
public class MerchantController {
    @Autowired
    MerchantService merchantService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("checkById")
    public JsonResult checkById(@RequestParam("id") Integer id){
         Merchant merchant = merchantService.selectById(id);
        return new JsonResult<>(0, "", merchant);
    }

    /**
     * 查询所有的商家列表
     * @return 返回前端处理数据，包含商家列表
     */

    @RequestMapping("selectAll")
    public JsonResult selectAll(){
        return new JsonResult<>(0, "", merchantService.selectAllMerchant());
    }

    /**
     * 分页查询
     * @param merchant 查询商家的条件类别
     * @param pageNum 第几页
     * @param pageSize 每页的元素数量
     * @return 返回前端处理数据，包含分页查询的分页信息
     */
    @RequestMapping("page")
    public JsonResult page(Merchant merchant,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        return new JsonResult<>(0, "", merchantService.page(merchant, pageNum, pageSize));
    }

    /**
     * 添加商家的接口
     * @param merchant 指定需要添加的商家，传入对象
     * @return 返回是否添加成功
     */
    @RequestMapping("add")
    public JsonResult add(Merchant merchant) {
        return new JsonResult<>(0, "", merchantService.addMerchant(merchant));
    }

    /**
     * 更新商家信息
     * @param merchant 传入前端处理好的对象form
     * @return 返回是否更新成功
     */
    @RequestMapping("update")
    public JsonResult update(Merchant merchant) {
        return new JsonResult(0, "", merchantService.updateMerchant(merchant));
    }

    /**
     * 根据id删除商家
     * @param id 输入的商家id
     * @return 返回是否删除成功的Json对象
     */
    @RequestMapping("remove")
    public JsonResult remove(Integer id) {
        return new JsonResult<>(0, "", merchantService.delete(id));
    }

    /**
     * 批量删除商家
     * @param ids 输入的id数组
     * @return 返回是否删除成功
     */
    @RequestMapping("removeIds")
    public JsonResult removeFromIds(@RequestBody List<Integer> ids) {
        return new JsonResult(0, "", merchantService.deleteFromIds(ids));
    }

    /**
     * 列出省份
     * @return 返回省份列表
     */
    @RequestMapping("listProvinces")
    public JsonResult listProvince(){
        return new JsonResult(0, "", merchantService.listProvince());
    }

    /**
     * 列出城市
     * @param provinceId 传入父元素
     * @return 返回父元素下所有的城市
     */
    @RequestMapping("listCities")
    public JsonResult listCities(@RequestParam("provinceId") Integer provinceId) {
        return new JsonResult(0, "", merchantService.listCities(provinceId));
    }

    /**
     * 列出区
     * @param cityId 传入父元素城市编号
     * @return 返回父元素喜爱所有的区
     */
    @RequestMapping("listDistricts")
    public JsonResult listDistrict(@RequestParam("cityId") Integer cityId) {
        return new JsonResult(0, "", merchantService.listDistrict(cityId));
    }

    /**
     * 根据省份id查询省份
     * @param provinceId 省份id
     * @return 返回省份对象
     */
    @RequestMapping("checkProvince")
    public JsonResult checkProvince(Integer provinceId) {
        return new JsonResult(0, "", merchantService.checkProvinceById(provinceId));
    }

    /**
     * 根据城市id查询城市
     * @param cityId 城市id
     * @return 返回城市对象
     */
    @RequestMapping("checkCity")
    public JsonResult checkCity(Integer cityId) {
        return new JsonResult<>(0, "", merchantService.checkCityById(cityId));
    }

    /**
     * 根据区的id查询区域对象
     * @param districtId 区域id
     * @return 发那会区域对象
     */
    @RequestMapping("checkDistrict")
    public JsonResult checkDistrict(Integer districtId) {
        return new JsonResult(0, "", merchantService.checkDistrictById(districtId));
    }

    /**
     * 批量通过Merchant的审核
     * @param ids merchantId列表
     * @return 返回更新的信息
     */
    @RequestMapping("passMerchants")
    public JsonResult passMerchants(@RequestBody List<Integer> ids) {
        return new JsonResult(0, "", merchantService.passMerchants(ids));
    }
}
